/**
 * 公告管理
 * @author williamJM
 * date 2018/04/28
 */

"use strict";


define(["app","config/pathConfig","layer"],function (app,pathConfig,layer) {

    app.registerController("ctrl.notice.list",function ($scope,$rootScope,$state,$http,DTOptionsBuilder, DTColumnDefBuilder, $compile) {

        $http.get(pathConfig.backstagePath.getPath() + "/notice")
            .then(function successCallback(response) {
                $scope.noticeList = response.data.data;
            });

        //查看公告详细信息
        $scope.open = function (notice) {
            localStorage.setItem("noticeInfo",JSON.stringify(notice));
            var index = layer.open({
                type: 2,
                closeBtn : "1",
                shade : 0,
                title : "公告",
                move : false,
                cancel : function () {
                    localStorage.removeItem("noticeInfo");
                },
                content: "views/notice/open-notice-page.html"
            });
            layer.full(index);
        };

        var httpDelete = function (noticeId) {
            return new Promise(function (success,error) {
                $http.delete(pathConfig.backstagePath.getPath() + "/notice/" + noticeId)
                    .then(function successCallback(response) {
                        success(response.data);
                    })
            })
        };

        //查看公告详细信息
        $scope.delete = function (noticeId) {
            layer.confirm('确定要删除当前的公告吗？', {
                icon: 3,
                title:'删除提示',
                btn: ['确定','取消']
            }, function () {
                httpDelete(noticeId).then(function (response) {
                    if(response.status === 200) {
                        layer.msg('删除成功！', {icon: 1,time:1500});
                        $state.reload("index.noticeList");
                        return;
                    }
                    layer.msg("服务器错误！", {icon: 2,time:1500});
                });
            });
        }



    })

});


